// pages/Health/Health.js
var app = getApp();
//HTTP请求request封装
var HttpRequest = require("../../http.js"); 
//CBC加密 
var CryptoJS = require("../../Crypto.js");
//RSA加密
var RSA = require("../../rsa.js");
//qrcode生成二维码
var QRCodeJS = require("../../utils/qrcode.js");
Page({

  /**
   * 页面的初始数据
   */
  data: { 
      name:'',//持卡人姓名
      cordId:'',//解码的身份证号码
      image:'',//二维码图片
      zjhm:'',//加密的身份证号码
  },
  getReq: HttpRequest.getReq,//GET数据请求函数
  postReq: HttpRequest.postReq,//POST数据请求
  header: HttpRequest.header,//header参数
  changeName:function(){
    wx.navigateTo({
       //type值为3时代表我的钱包传值
      url: '../url/url?type=3'
    })
  },
  Bank:function(){
    wx.navigateTo({
      url: '../micro/micro'
    })
  },
  personalfiles:function(){
    wx.navigateTo({
      url: '../FileView/Personal/Personal'
    })
  },
  followfile:function(){
    wx.navigateTo({
      url: '../FileView/FollowFile/FollowFile'
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    console.log(getApp().globalData.LoginStatus);
    
    if (getApp().globalData.LoginStatus === false){
      wx.showModal({
        title: '温馨提示',
        content: '您还未登录，请先登录！',
        showCancel: false,
        success: function (v) {
          wx.redirectTo({
            url: '../logIn/logIn'
          })
        }
      })
    }else{
      const login_Info = JSON.parse(wx.getStorageSync('login_Information')); //登录基础信息
      const headArray = wx.getStorageSync('UserLogin');
      const headmode = {
        'Accept': 'application/json',
        "Content-Type": "application/x-www-form-urlencoded",
        "headMode": headArray,
        "token": login_Info.token,
      };
      const infoUrl = '/resident/healthcard/info';
      
      //随机私钥  
      const key = CryptoJS.enc.Utf8.parse(app.globalData.PrivateKey);
      console.log("随机私钥:" + key);

      //加密偏移量
      const options = {
        iv: CryptoJS.enc.Utf8.parse("bskyapp123456789"),
        mode: CryptoJS.mode.CBC,
        padding: CryptoJS.pad.Pkcs7
      };
      HttpRequest.getReq(infoUrl, getApp().globalData.data , headmode, function (res, code) {
        console.log(res);
        //身份证号码加密
        wx.setStorage({ key: "IdNumber", data: res.zjhm })
        //CBC解密卡号
        var decryptedData = CryptoJS.AES.decrypt(res.zjhm, key, options);
        var decryptedStr = decryptedData.toString(CryptoJS.enc.Utf8);
        console.log('CBC解密：', decryptedStr);
        const cordId = decryptedStr;
        const cordId_hm = cordId.substr(0, 3) + '****' + cordId.substr(14);
        //CBC解密姓名
        var decryptedName = CryptoJS.AES.decrypt(res.realName, key, options);
        var decryptedStrName = decryptedName.toString(CryptoJS.enc.Utf8);
        console.log('CBC解密：', decryptedStrName);
        const realName = decryptedStrName
        /*
        --------新版本发布启用
        */
        //CBC解密二维码图片
        var Base64 = res.base64;
        //console.log(Base64.search("\n") == -1);
        if (!(Base64.search("\n") == -1)) {
          Base64 = Base64.replace("\n", "")
        }
        //console.log("BaseImage:", Base64);
        var decryptedimage = CryptoJS.AES.decrypt(Base64, key, options);
        var decryptedStrimage = decryptedimage.toString(CryptoJS.enc.Utf8);
        console.log('CBC解密：', decryptedStrimage);
        const baseImage = decryptedStrimage;

        /**
       * 绘制带logo二维码
       * @param url        二维码字符串 如 https://github.com/xlsn0w
       * @param canvas-id  画布ID 如 logoQRCode
       * @param width      二维码宽度 如 275
       * @param height     二维码高度 如 275
       * @param logo       二维码logo 如 /images/xlsn0w.png
       */
        QRCodeJS.qrApi.draw(baseImage, "logoQRCode", 70, 70, null, '../../pages/image/redTen.jpg');

        that.setData({
          //登录基础信息回写
          name: realName,//用户姓名
          cordId: cordId_hm,
          image: res.base64,
          zjhm: res.zjhm,
        })
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})